package com.eascs.crm.nsEditDetail.service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.ContactsInfoDao;
import com.eascs.crm.nsEditDetail.entity.ContactsInfoEntity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ContactsInfoService {
	@Autowired
	private ContactsInfoDao contactsInfoDao;

	public ContactsInfoEntity save(ContactsInfoEntity entity) {
		int effectRow = contactsInfoDao.insert(entity);
		if (effectRow > 0) {
			return entity;
		}
		return null;
	}

	public ContactsInfoEntity getByLoanNo(String loanNo) {
		String sql = "SELECT * from contacts_info t where t.is_del='0' and t.loanapply_no = ?";
		return contactsInfoDao.findEntity(sql, ContactsInfoEntity.class, loanNo);
	}
	
	public int getByLoanNoNum(String loanNo) {
		String sql = "SELECT count(*) from contacts_info t where t.is_del='0' and t.loanapply_no = ?";
		return contactsInfoDao.findInteger(sql, loanNo);
	}

	public ContactsInfoEntity getById(String id) {
		return contactsInfoDao.getById(ContactsInfoEntity.class, id);
	}

	public int update(ContactsInfoEntity entity) {
		return contactsInfoDao.updateById(entity);
	}

	/**
	 * @Description:
	 *               <p>
	 *               紧急联系人列表查询--脱敏（待办、已办）
	 *               </p>
	 * @author LMX
	 */
	public PageResult<Object[]> findContactsListInfo(String loanapplyNo,Integer showPage,String type, String approvalStatus) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, showPage);
		StringBuilder sql = new StringBuilder();
		
		if(StringUtil.isNotBlank(approvalStatus)){
			if (!"UNDO".equals(approvalStatus)) {
				sql.append("SELECT t.id,t.Contacts_name,t.Type,CONCAT( LEFT (t.Address, 6), '******') AS Address,CONCAT( LEFT (t.home_phone, 3), '****', RIGHT (t.home_phone, 3)) AS home_phone from contacts_info t " +
						" where t.is_del = 0 AND t.loanapply_no=?");
			}else {
				sql.append("SELECT t.id,t.Contacts_name,t.Type,t.Address AS Address,t.home_phone AS home_phone from contacts_info t " +
						" where t.is_del = 0 AND t.loanapply_no=?");
			}
		}else {
			if (!"detail".equals(type)) {
				sql.append("SELECT t.id,t.Contacts_name,t.Type,CONCAT( LEFT (t.Address, 6), '******') AS Address,CONCAT( LEFT (t.home_phone, 3), '****', RIGHT (t.home_phone, 3)) AS home_phone from contacts_info t " +
						" where t.is_del = 0 AND t.loanapply_no=?");
			}else{
				sql.append("SELECT t.id,t.Contacts_name,t.Type,t.Address AS Address,t.home_phone AS home_phone from contacts_info t " +
						" where t.is_del = 0 AND t.loanapply_no=?");
			}
		}
		
		sqlPageQuery.setQuerySql(sql.toString());
		sqlPageQuery.addParam(loanapplyNo);
		PageResult<Object[]> contactsPage = contactsInfoDao.findDataWithPage(sqlPageQuery);
		return contactsPage;
	}
	
	public int delete(String id){
		String sql = "update contacts_info set is_del = 1 where id = ?";
		return contactsInfoDao.execute(sql, id);
	}
}
